#!/bin/bash

# postinst script for system-bhyve-tests
#
# see: dh_installdeb(1)

#set -e

# summary of how this script can be called:
#        * <postinst> \`configure' <most-recently-configured-version>
#        * <old-postinst> \`abort-upgrade' <new version>
#        * <conflictor's-postinst> \`abort-remove' \`in-favour' <package>
#          <new-version>
#        * <postinst> \`abort-remove'
#        * <deconfigured's-postinst> \`abort-deconfigure' \`in-favour'
#          <failed-install-package> <version> \`removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

PATH=/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

if [ "${BASEDIR:=/}" = "/" ]; then
    BASEDIR=""
else
    BASEDIR_OPT="-b $BASEDIR"
fi

case "$1" in
    configure)
	chmod 0444 "$BASEDIR/opt/bhyve-tests/runfiles/default.run"
	chown root:bin "$BASEDIR/opt/bhyve-tests/runfiles/default.run"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/bin/bhyvetest"
	chown root:bin "$BASEDIR/opt/bhyve-tests/bin/bhyvetest"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/inst_emul/cpuid"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/inst_emul/cpuid"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/inst_emul/exit_paging"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/inst_emul/exit_paging"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/inst_emul/imul"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/inst_emul/imul"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/inst_emul/page_dirty"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/inst_emul/page_dirty"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/inst_emul/rdmsr"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/inst_emul/rdmsr"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/inst_emul/triple_fault"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/inst_emul/triple_fault"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/inst_emul/wrmsr"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/inst_emul/wrmsr"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/kdev/vatpit_freq"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/kdev/vatpit_freq"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/kdev/vhpet_freq"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/kdev/vhpet_freq"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/kdev/vlapic_freq"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/kdev/vlapic_freq"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/kdev/vlapic_freq_periodic"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/kdev/vlapic_freq_periodic"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/kdev/vlapic_mmio_access"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/kdev/vlapic_mmio_access"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/kdev/vlapic_msr_access"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/kdev/vlapic_msr_access"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/kdev/vpmtmr_freq"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/kdev/vpmtmr_freq"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/mevent/lists_delete"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/mevent/lists_delete"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/mevent/read_disable"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/mevent/read_disable"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/mevent/read_pause"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/mevent/read_pause"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/mevent/read_requeue"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/mevent/read_requeue"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/mevent/vnode_file"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/mevent/vnode_file"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/mevent/vnode_zvol"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/mevent/vnode_zvol"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/viona/interface_version"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/viona/interface_version"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/auto_destruct"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/auto_destruct"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/check_iommu"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/check_iommu"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/cpuid_ioctl"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/cpuid_ioctl"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/datarw_constraints"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/datarw_constraints"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/default_capabs"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/default_capabs"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/drv_hold"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/drv_hold"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/fpu_getset"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/fpu_getset"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/import_vlapic"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/import_vlapic"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/interface_version"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/interface_version"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/legacy_destruct"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/legacy_destruct"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/mem_devmem"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/mem_devmem"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/mem_partial"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/mem_partial"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/mem_seg_map"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/mem_seg_map"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/pause_resume"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/pause_resume"
	chmod 0555 "$BASEDIR/opt/bhyve-tests/tests/vmm/self_destruct"
	chown root:bin "$BASEDIR/opt/bhyve-tests/tests/vmm/self_destruct"
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/kernel/drv/vmm_drv_test.conf ] && rm -f $BASEDIR/usr/kernel/drv/vmm_drv_test.conf
	else
	    chmod 0644 "$BASEDIR/usr/kernel/drv/vmm_drv_test.conf"
	    chown root:bin "$BASEDIR/usr/kernel/drv/vmm_drv_test.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/kernel/drv/amd64/vmm_drv_test ] && rm -f $BASEDIR/usr/kernel/drv/amd64/vmm_drv_test
	else
	    chmod 0755 "$BASEDIR/usr/kernel/drv/amd64/vmm_drv_test"
	    chown root:bin "$BASEDIR/usr/kernel/drv/amd64/vmm_drv_test"
	fi
	[ "$ZONEINST" = "1" ] || (grep -c "^vmm_drv_test " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m '* 0666 root sys' -p 'read_priv_set=sys_devices write_priv_set=sys_devices' vmm_drv_test ) )
	
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac



# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

